package br.edu.unicentro.mymib.mbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.edu.unicentro.mymib.base.EntityManagerService;
import br.edu.unicentro.mymib.entity.Post;
import br.edu.unicentro.mymib.entity.Usuario;
import br.edu.unicentro.mymib.mbean.entity.ComentarioEntity;
import br.edu.unicentro.mymib.mbean.entity.PostEntity;

public class PostMBean {

	private List<PostEntity> posts;
	private List<ComentarioEntity> comentarios;
	private boolean windowNewPostVisible = false;
	private String tag1, tag2, tag3;
	private String conteudoPost = "";
	private String tituloPost;

	public PostMBean() {
		posts = new ArrayList<PostEntity>();
		comentarios = new ArrayList<ComentarioEntity>();
	}

	public void abrirWindow(ActionEvent event) {
		this.windowNewPostVisible = true;
	}

	public boolean isWindowNewPostVisible() {
		return windowNewPostVisible;
	}

	public void fecharWindow(ActionEvent event) {
		this.windowNewPostVisible = false;
		clear();
	}

	private void clear() {
		this.tituloPost = "";
		this.tag1 = "";
		this.tag2 = "";
		this.tag3 = "";
		this.conteudoPost = "";

	}

	public void submeter(ActionEvent event) {
		Usuario u = Usuario.recuperarUsuarioLogado();
		Post p = new Post(tituloPost, conteudoPost);
		if (canAddTag(tag1)) {
			p.adicionarTag(tag1);
		}
		if (canAddTag(tag2)) {
			p.adicionarTag(tag2);
		}
		if (canAddTag(tag3)) {
			p.adicionarTag(tag3);
		}
		try {
			u.adicionarPost(p);
			EntityManagerService.salvar(u);
		} catch (Exception e) {
			e.printStackTrace();
		}
		clear();
		this.windowNewPostVisible = false;
	}

	private boolean canAddTag(String tag) {
		if (tag != null) {
			if (!tag.equals("")) {
				return true;
			}
		}
		return false;
	}

	public List<PostEntity> getPosts() {
		posts = new ArrayList<PostEntity>();

		PostEntity pe;
		for (Post p : Post.buscarTodos()) {
			pe = new PostEntity();
			pe.setPost(p);
			posts.add(0, pe);
			//posts.add(pe);
		}

		return posts;
	}

	public List<PostEntity> getPostDeUsuario(Usuario usuario) {
		posts = new ArrayList<PostEntity>();

		PostEntity pe;
		List<Post> list = Post.buscarPorEmailDeUsuario(usuario.getEmail());
		for (int i = list.size() - 1; i >= 0; i--) {
			pe = new PostEntity();
			pe.setPost(list.get(i));
			posts.add(pe);
		}

		return posts;

	}

	public List<PostEntity> getPostsDeUsuarioLogado() {
		posts = new ArrayList<PostEntity>();
		Usuario u = Usuario.recuperarUsuarioLogado();
		if (u != null) {
			PostEntity pe;
			for (Post p : u.getPosts()) {
				pe = new PostEntity();
				pe.setPost(p);
				posts.add(pe);
			}
		}

		return posts;
	}

	public String getTag1() {
		return tag1;
	}

	public void setTag1(String tag1) {
		this.tag1 = tag1;
	}

	public String getTag2() {
		return tag2;
	}

	public void setTag2(String tag2) {
		this.tag2 = tag2;
	}

	public String getTag3() {
		return tag3;
	}

	public void setTag3(String tag3) {
		this.tag3 = tag3;
	}

	public String getConteudoPost() {
		return conteudoPost;
	}

	public void setConteudoPost(String conteudoPost) {
		this.conteudoPost = conteudoPost;
	}

	public String getTituloPost() {
		return tituloPost;
	}

	public void setTituloPost(String tituloPost) {
		this.tituloPost = tituloPost;
	}

	public List<ComentarioEntity> getComentarios() {

		return comentarios;
	}

	public String getLength() {
		return String.valueOf((200 - conteudoPost.length()))
				+ " caracteres restantes.";
	}

	public void contentSize(FacesContext fco, UIComponent uic, Object obj) {

	}
}
